﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

using ConsolidationTool.Common;
using ConsolidationTool.WebParts.ControlTemplates;
using System.Data;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.IO;

namespace ConsolidationTool.WebParts.Layouts.CReport.Forecast
{
    public partial class TSBR : FSReportBase
    {
        public string strContent = string.Empty, strHeader=string.Empty;
        string year, month;
        public override ControlTemplates.CommonEntitySelector TopSelector
        {
            get
            {
                return commonEntitySelector as ControlTemplates.CommonEntitySelector;
            }
        }

        public override void BeforLoad()
        {
            if (!IsPostBack)
            {
                Hid_ActualSource.Value = Request.QueryString["Actual"];
                Hid_Forecast.Value = Request.QueryString["Forecast"];
                if (!Automatic)
                {
                    year = Common.BusinessService.GetConfiguration(Constants.Flash_Input_Year);
                    month = Common.BusinessService.GetConfiguration(Constants.Flash_Input_Month);
                }
            }
        }

        //创建表头
        public string CreateTableHead()
        {
            string strHeader1 = "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
            #region --第一列
            year = TopSelector.yearTextBox.Text == "" ? "0" : TopSelector.yearTextBox.Text;
            month = TopSelector.monthTextBox.Text == "" ? "0" : TopSelector.monthTextBox.Text;
            
            for (int i = 1; i <= Convert.ToInt32(month); i++)
            {
                strHeader1 += "<td colspan='2'>" + year + "." + i.ToString("D2") + "</td>";
            }
            switch (month)
            {
                case "10":
                    strHeader1 += "<td colspan='2'>" + year + "." + (Convert.ToInt32(month) + 1).ToString("D2") + "</td>";
                    strHeader1 += "<td colspan='2'>" + year + "." + (Convert.ToInt32(month) + 2).ToString("D2") + "</td>";
                    strHeader1 += "<td colspan='2'>" + (Convert.ToInt32(year) + 1) + ".01 </td>";
                    break;
                case "11":
                    strHeader1 += "<td colspan='2'>" + year + "." + (Convert.ToInt32(month) + 1).ToString("D2") + "</td>";
                    strHeader1 += "<td colspan='2'>" + (Convert.ToInt32(year) + 1) + ".01 </td>";
                    strHeader1 += "<td colspan='2'>" + (Convert.ToInt32(year) + 1) + ".02 </td>";
                    break;
                case "12":
                    strHeader1 += "<td colspan='2'>" + (Convert.ToInt32(year) + 1) + ".01 </td>";
                    strHeader1 += "<td colspan='2'>" + (Convert.ToInt32(year) + 1) + ".02 </td>";
                    strHeader1 += "<td colspan='2'>" + (Convert.ToInt32(year) + 1) + ".03 </td>";
                    break;
                default:
                    strHeader1 += "<td colspan='2'>" + year + "." + (Convert.ToInt32(month) + 1).ToString("D2") + "</td>";
                    strHeader1 += "<td colspan='2'>" + year + "." + (Convert.ToInt32(month) + 2).ToString("D2") + "</td>";
                    strHeader1 += "<td colspan='2'>" + year + "." + (Convert.ToInt32(month) + 3).ToString("D2") + "</td>";
                    break;
            }
            strHeader1 += "</tr>";
            #endregion
            #region --第二列
            string strHeader2 = "<tr><td></td><td></td><td></td>";
            string strHeader3 = "<tr><td>Account Code</td><td>RGC</td><td>Description</td>";
            for (int i = 1; i <= Convert.ToInt32(month); i++)
            {
                strHeader2 += "<td colspan='2'>Actual</td>";
                strHeader3 += "<td>Qty</td><td>Turnover</td>";
            }
            //Forecast数据列
            for (int i = 0; i < 3; i++)
            {
                strHeader2 += "<td colspan='2'>Forecast</td>";
                strHeader3 += "<td>Qty</td><td>Turnover</td>";
            }
            //for (int i = 1; i <= 9 - Convert.ToInt32(month); i++)
            //{
            //    strHeader1 += "<td></td>";
            //}
            strHeader2 += "</tr>";
            strHeader3 += "</tr>";
            #endregion
            return strHeader1 + strHeader2 + strHeader3;
        }

        public override void GenerateReport_Click(object sender, EventArgs e)
        {
            string ActualSource = Hid_ActualSource.Value; //Actual数据源
            string TempUrl = Hid_Forecast.Value + ".js"; // 模版js
            string ForecastSource = "Forecast_" + Hid_Forecast.Value;// Forecast数据源
            string ChildSource = "Sub " + Hid_ActualSource.Value;//子项数据源
            strHeader = CreateTableHead();

            int month = TopSelector.monthTextBox.Text == "" ? 0 : Convert.ToInt32(TopSelector.monthTextBox.Text);
            DataTable actualdt = ForecastData.GetActualDt(TopSelector.lbCoCd.Text, TopSelector.yearTextBox.Text, month, ActualSource);
            var list = ForecastData.GetName(Server.MapPath("DataColumn/" + TempUrl));//获取js数据字段
            for (int m = 0; m < list.Count; m++)
            {
                string code = list[m].code.Trim();
                string name = list[m].desc;
                if (!string.IsNullOrEmpty(code) && !string.IsNullOrEmpty(name))
                {
                    List<fssubentity> subForecast;
                    string strForecast = ForecastData.GetForecast(TopSelector.lbCoCd.Text, TopSelector.yearTextBox.Text, month.ToString(), ForecastSource, code, Hid_Forecast.Value, out subForecast);//Forecast数据

                    strContent += GetChildList(TopSelector.lbCoCd.Text, TopSelector.yearTextBox.Text, month, ChildSource, code.Replace("CM_", string.Empty), Hid_Forecast.Value, subForecast);
                    strContent += "<tr>";
                    strContent += "<td>" + code.Replace("CM_", string.Empty) + "</td>";
                    strContent += "<td colspan='2'>" + name + "</td>";
                    for (int i = 0; i < actualdt.Rows.Count; i++)
                    {
                        #region --
                        strContent += "<td>" + actualdt.Rows[i]["CM_QTY_" + code] + "</td>";
                        strContent += "<td>" + actualdt.Rows[i]["CM_T_" + code] + "</td>";
                        #endregion
                    }
                    //填充Forecast前
                    for (int i = 0; i < month - actualdt.Rows.Count; i++)
                    {
                        strContent += "<td>0</td><td>0</td>";
                    }
                    string startd = "<td></td><td></td><td></td><td></td><td></td><td></td>";
                    strContent += strForecast == "" ? startd : strForecast;
                    //填充Forecast后
                    //for (int j = 1; j <= 9 - month; j++)
                    //{
                    //    strContent += "<td></td>";
                    //}
                    strContent += "</tr>";
                }
                else
                {
                    #region --
                    if (!string.IsNullOrEmpty(name))
                    {
                        strContent += "<tr>";
                        strContent += "<td></td><td colspan='2'><span class='fs-display-title' style='font-weight: 700'>" + name + "</span></td><td colspan='" + (month * 2 + 6) + "'></td>";
                        strContent += "</tr>";
                    }
                    else
                    {
                        strContent += "<tr>";
                        strContent += "<td></td><td colspan='" + (month * 2 + 8) + "'></td>";
                        strContent += "</tr>";
                    }
                    #endregion
                }
            }
        }

        /// <summary>
        /// 获取子项
        /// </summary>
        /// <param name="coCode">公司code</param>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <param name="listName">子项数据源</param>
        /// <param name="accountCode">父类code</param>
        /// <returns></returns>
        private string GetChildList(string coCode, string year, int month, string listName, string accountCode, string type, List<fssubentity> subForecast)
        {
            string strChild = "";
            DataTable dt = ForecastData.GetSubActualDs(coCode, year, month, listName, accountCode, type);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string rgc = dt.Rows[i]["RGC"].ToString();
                    string name = Common.BusinessService.GetRGCDescriptionByRGC(rgc);
                    strChild += "<tr><td></td>";
                    strChild += "<td>" + rgc + "</td>";
                    strChild += "<td>" + name + "</td>";
                    for (int j = 1; j <= month; j++)
                    {
                        if (string.IsNullOrEmpty(dt.Rows[i]["Qty" + j].ToString()))
                            strChild += "<td>0</td>";
                        else
                            strChild += "<td>" + dt.Rows[i]["Qty" + j] + "</td>";
                        if (string.IsNullOrEmpty(dt.Rows[i]["Turnover" + j].ToString()))
                            strChild += "<td>0</td>";
                        else
                            strChild += "<td>" + dt.Rows[i]["Turnover" + j] + "</td>";
                    }
                    strChild += strForecast(subForecast, name) == "" ? "<td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td>" : strForecast(subForecast, name);
                    strChild += "</tr>";
                }
            }
            return strChild;
        }

        /// <summary>
        /// 子项Forecast数据
        /// </summary>
        /// <param name="subForecast"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private string strForecast(List<fssubentity> subForecast, string name)
        {
            string strForecast = "";
            if (subForecast != null)
            {
                for (int m = 0; m < subForecast.Count; m++)
                {
                    if (subForecast[m].RGCDescription == name)
                    {
                        strForecast += "<td>" + subForecast[m].M_1_Qty + "</td><td>" + subForecast[m].M_1_T + "</td>";
                        strForecast += "<td>" + subForecast[m].M_2_Qty + "</td><td>" + subForecast[m].M_2_T + "</td>";
                        strForecast += "<td>" + subForecast[m].M_3_Qty + "</td><td>" + subForecast[m].M_3_T + "</td>";
                    }
                }
            }
            return strForecast;
        }
    }
}
